﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using MedicWave.Visualization3d;

namespace ScattterplotTestForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent(); 
            this.Load += new EventHandler(Form1_Load);

        }

        void Form1_Load(object sender, EventArgs e)
        {
            ScatterplotSettings settings = new ScatterplotSettings();
            ScatterplotData dataSP = ReadFileDataSP("sample_scatterplot3D.txt",out settings);
            dataSP.PlotType = ScatterplotType.Normal;
            //dataSP. = "Plot1";
            settings.Title = "Plot1";
            settings.ShowGrid = true;
            //settings.ShowCaseNames = false;
            settings.ShowLegend = true;
            
            //settings.XLabel = "MZ:2023.7";
            //settings.YLabel = "MZ:2092.4";
            //settings.ZLabel = "MZ:2139.3";
            //settings.PlotProjectionType = ProjectionType.Orthographic;

            widgetScatterplot3D1.SetData(dataSP,settings);

            
        }

        private ScatterplotData ReadFileDataSP(string sFileName,out ScatterplotSettings settings)
        {
            StreamReader file;
            ArrayList arrList = new ArrayList();
            ScatterplotData data = new ScatterplotData();
            data.CaseNames = new List<string>();
            data.PlotData = new List<ScatterplotPoint>();
            data.CasesClassLabels = new List<uint>();
            settings = new ScatterplotSettings();
            
            try
            {
                file = new StreamReader(sFileName);
                string line;
                Scan tmpPV = null;//= new Scan();                
                bool first = true;
                while (!file.EndOfStream)
                {
                    line = file.ReadLine();
                    if (line.Contains("case_name"))
                    {
                        string[] headers = line.Split(new char[] { ' ' });
                        if (headers.Length > 3)
                        {
                            settings.XLabel = headers[1].Trim('"');
                            settings.YLabel = headers[2].Trim('"');
                            settings.ZLabel = headers[3].Trim('"'); 
                        }
                        first = false;
                        
                    }
                    else if(first)
                    {
                        string[] headers = line.Split(new char[] { ' ' });
                        if (headers.Length > 2)
                        {
                            settings.XLabel = headers[0].Trim('"');
                            settings.YLabel = headers[1].Trim('"');
                            settings.ZLabel = headers[2].Trim('"');
                        }
                        first = false;
                    }
                    else
                    {
                        line = line.Replace(".", ",");
                        string[] sArr = line.Split(' ');
                        sArr[0] = sArr[0].Trim('"');
                        data.CaseNames.Add(sArr[0]);
                        ScatterplotPoint point = new ScatterplotPoint();
                        point.X = (float)System.Convert.ToDouble(sArr[1]);
                        point.Y = (float)System.Convert.ToDouble(sArr[2]);
                        point.Z = (float)System.Convert.ToDouble(sArr[3]);
                        data.PlotData.Add(point);
                        if(sArr.Length>4)
                        data.CasesClassLabels.Add(System.Convert.ToUInt32(sArr[4]));

                    }
                }
            }
            catch (System.Exception e)
            {
                MessageBox.Show(e.Message);
            }


            return data;
        }
        ScatterplotType currentType = ScatterplotType.Normal;
        private void butTemp_Click(object sender, EventArgs e)
        {
            if (currentType != ScatterplotType.BiPlot)
            {
                ScatterplotSettings settings = new ScatterplotSettings();
                ScatterplotData dataSP = ReadFileDataSP("biplot3D_example1.csv", out settings);
                dataSP.PlotType = ScatterplotType.BiPlot;
                //dataSP. = "Plot1";
                settings.Title = "Plot1";
                settings.ShowGrid = true;                
                settings.ShowLegend = true;
                //settings.XLabel = "MZ:2023.7";
                //settings.YLabel = "MZ:2092.4";
                //settings.ZLabel = "MZ:2139.3";
                //settings.PlotProjectionType = ProjectionType.Orthographic;

                widgetScatterplot3D1.SetData(dataSP, settings);
                currentType = ScatterplotType.BiPlot;
            }
            else
            {
                ScatterplotSettings settings = new ScatterplotSettings();
                ScatterplotData dataSP = ReadFileDataSP("sample_scatterplot3D.txt", out settings);
                dataSP.PlotType = ScatterplotType.Normal;
                //dataSP. = "Plot1";
                settings.Title = "Plot1";
                settings.ShowGrid = true;                
                settings.ShowLegend = true;

                //settings.XLabel = "MZ:2023.7";
                //settings.YLabel = "MZ:2092.4";
                //settings.ZLabel = "MZ:2139.3";
                //settings.PlotProjectionType = ProjectionType.Orthographic;

                widgetScatterplot3D1.SetData(dataSP, settings);
                currentType = ScatterplotType.Normal;
            }
        }
    }
}
